package com.example.hp.yuejian5.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;


import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.RetrofitListener.RetrofitListener;
import com.example.hp.yuejian5.bean.AttentionBean;
import com.example.hp.yuejian5.bean.ClubBean;
import com.example.hp.yuejian5.bean.IsAttentionBean;
import com.example.hp.yuejian5.model.Club_DetailModel;
import com.squareup.picasso.Picasso;

import static com.example.hp.yuejian5.other.url.IMAGES;

public class Club_DetailActivity extends AppCompatActivity {

    private String i_id, u_id;
    private Context context;
    private RadioButton course, clubvideo;
    //private ClubMasterFragment clubVideoFragment;
   // private CourseFragment courseFragment;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    private RetrofitListener<ClubBean> listener;
    private RetrofitListener<IsAttentionBean> isattentionBeanRetrofitListener;
    private RetrofitListener<AttentionBean> attentionBeanRetrofitListener, cancelattentionBeanRetrofitListener;

    private ImageView club_detail_pic;
    private Toolbar toolbar;
    private TextView toolbartitle;
    private int flagattention;
    private ImageView isattentionclub;
    private SharedPreferences sp;
    private TextView club_detail_name, club_detail_tel, club_detail_intro, club_detail_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_club__detail);
        setSupportActionBar(toolbar);
        Intent intent = getIntent();
        i_id = (String) intent.getStringExtra("c_uid");
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        u_id = sp.getString("userid", null);
        initView();
        //initEvents();
        listener = new RetrofitListener<ClubBean>() {
            @Override
            public void onSuccess(ClubBean clubBean, int flag) {
                club_detail_name.setText(clubBean.getClub_name());
                club_detail_tel.setText(clubBean.getClub_tel());
                club_detail_address.setText(clubBean.getClub_address());
                club_detail_intro.setText(clubBean.getClub_intro());
                Picasso.with(context).load( IMAGES+ clubBean.getClub_pic()).into(club_detail_pic);
            }

            @Override
            public void onFail() {

            }
        };
        isattentionBeanRetrofitListener = new RetrofitListener<IsAttentionBean>() {
            @Override
            public void onSuccess(IsAttentionBean isAttentionBean, int flag) {
                flagattention = Integer.parseInt(isAttentionBean.getIsattention());
                if (isAttentionBean.getIsattention().equals("1")) {
                    isattentionclub.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.aaa));

                }

            }

            @Override
            public void onFail() {

            }
        };

        attentionBeanRetrofitListener = new RetrofitListener<AttentionBean>() {
            @Override
            public void onSuccess(AttentionBean attentionBean, int flag) {
                if (attentionBean.getSuccess().equals("1")) {
                    flagattention = 1;
                    Toast.makeText(Club_DetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {

            }
        };

        cancelattentionBeanRetrofitListener = new RetrofitListener<AttentionBean>() {
            @Override
            public void onSuccess(AttentionBean attentionBean, int flag) {
                if (attentionBean.getSuccess().equals("1")) {
                    flagattention = 0;
                    Toast.makeText(Club_DetailActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {

            }
        };
        clubDetail();
       // isAttentionClub();
    }

    private void initView() {
        club_detail_pic = findViewById(R.id.club_detail_pic);
        club_detail_name = findViewById(R.id.club_detail_name);
        club_detail_tel = findViewById(R.id.club_detail_tel);
        club_detail_intro = findViewById(R.id.club_detail_intro);
        club_detail_address = findViewById(R.id.club_detail_address);
        toolbar = findViewById(R.id.club_detail_toolbar);
        toolbartitle = findViewById(R.id.club_detail_toolbar_text);
        //isattentionclub = findViewById(R.id.is_attrntion_club);
    }

//    private void initEvents() {
//        course.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    if (courseFragment == null) {
//                        courseFragment = new CourseFragment();
//                    }
//                    fragmentTransaction = fragmentManager.beginTransaction();
//                    fragmentTransaction.replace(R.id.detail_framelayout, courseFragment);
//                    fragmentTransaction.commit();
//                }
//            }
//        });
//
//        clubvideo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    if (clubVideoFragment == null) {
//                        clubVideoFragment = new ClubMasterFragment();
//                    }
//                    fragmentTransaction = fragmentManager.beginTransaction();
//                    fragmentTransaction.replace(R.id.detail_framelayout, clubVideoFragment);
//                    fragmentTransaction.commit();
//                }
//            }
//        });

//        fragmentManager = getSupportFragmentManager();
//        course.setChecked(true);
//
//
//        isattentionclub.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (flagattention == 1) {
//                    cancelAttentionClub();
//                    isattentionclub.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.bbb));
//                } else {
//                    attentionClub();
//                    isattentionclub.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.aaa));
//                }
//            }
//        });
//    }


    private void clubDetail() {
        Club_DetailModel club_detailModel = new Club_DetailModel();
        club_detailModel.clubDetail(i_id, listener);
    }


//    private void isAttentionClub() {
//        IsAttentionClubModel isAttentionClubModel = new IsAttentionClubModel();
//        isAttentionClubModel.isAttentionclub(u_id, i_id, isattentionBeanRetrofitListener);
//    }
//
//    private void cancelAttentionClub() {
//        CancelAttentionClubModel cancelAttentionClubModel = new CancelAttentionClubModel();
//        cancelAttentionClubModel.cancelAttentionClub(u_id, i_id, cancelattentionBeanRetrofitListener);
//    }
//
//    private void attentionClub() {
//        AttentionClubModel attentionClubModel = new AttentionClubModel();
//        attentionClubModel.attentionClub(u_id, i_id, attentionBeanRetrofitListener);
//    }

}
